{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-11T12:19:09.294055700Z",
     "start_time": "2025-08-11T12:19:03.376662500Z"
    }
   },
   "outputs": [],
   "source": [
    "from gs_quant.instrument import IRSwaption, IRSwap, IRCapFloor\n",
    "from gs_quant.common import AggregationLevel\n",
    "from gs_quant.markets.portfolio import Portfolio\n",
    "from gs_quant.risk import IRVanna, IRVolga\n",
    "from gs_quant.session import Environment, GsSession"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-08-11T12:19:16.393303500Z",
     "start_time": "2025-08-11T12:19:11.738322600Z"
    }
   },
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Solve for Vanna and Volga\n",
    "Size swap to match swaption vanna"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": [
     "Instrument - Solving - Size one trade by the risk of another trade"
    ],
    "ExecuteTime": {
     "end_time": "2025-08-11T12:19:21.346721500Z",
     "start_time": "2025-08-11T12:19:18.268449900Z"
    }
   },
   "outputs": [],
   "source": [
    "port = Portfolio(\n",
    "    (\n",
    "        IRSwaption('Pay', '5y', 'USD', expiration_date='1y', buy_sell='Sell', name='payer_swaption'),\n",
    "        IRSwap(\n",
    "            'Receive',\n",
    "            '5y',\n",
    "            'USD',\n",
    "            fixed_rate='atm',\n",
    "            notional_amount='=solvefor([payer_swaption].risk.IRDeltaParallel,bp)',\n",
    "            name='hedge1',\n",
    "        ),\n",
    "    )\n",
    ")\n",
    "port.resolve()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "port.calc(IRVanna).to_frame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "port.calc(IRVolga).to_frame()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Swaption (return) is non-linear (to interest change), that's why we can calculate Vanna and Volga for it, while IRSwap is linear and have it as zero."
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "We can also check IRVanna and IRVolga with IRCapFloor instruments, which return new options for every defined period. "
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "cap = IRCapFloor(\n",
    "    cap_floor=\"Cap\",\n",
    "    notional_currency=\"USD\",\n",
    "    notional_amount=1000,\n",
    "    effective_date='2025-07-18',\n",
    "    termination_date='2030-07-18',\n",
    "    floating_rate_option='USD-LIBOR-BBA',\n",
    "    floating_rate_designated_maturity='3m',\n",
    "    floating_rate_frequency='3m',\n",
    "    strike=0.015,  # 1. 5% floor rate\n",
    ")\n",
    "cap.resolve(in_place=False).to_dict()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "cap.calc(IRVanna)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "cap.calc(IRVanna(aggregation_level=AggregationLevel.Type))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "cap.calc(IRVolga)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "cap.calc(IRVolga(aggregation_level=AggregationLevel.Type))"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
